package top.yunp.sstws;

import javax.swing.*;
import java.awt.*;

public class FirstView extends JFrame {

    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;


    public FirstView() throws HeadlessException {
        super("Sync swing theme with system");
        buildUI();
        showWindow();
    }

    private JPanel createFormLine(String formLabel) {
        var line = new JPanel(new BorderLayout(10, 10));
        var label = new JLabel(formLabel);
        label.setPreferredSize(new Dimension(80, 30));
        line.add(label, BorderLayout.LINE_START);
        line.add(new JTextField(), BorderLayout.CENTER);
        line.setMaximumSize(new Dimension(Integer.MAX_VALUE, 36));
        return line;
    }

    private void buildUI() {
        var root = getRootPane();
        root.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        var container = getContentPane();
        container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
        var titleLabel = new JLabel("User login");
        titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(titleLabel);
        container.add(Box.createRigidArea(new Dimension(10, 10)));
        container.add(createFormLine("Email:"));
        container.add(Box.createRigidArea(new Dimension(10, 10)));
        container.add(createFormLine("User name:"));
        container.add(Box.createRigidArea(new Dimension(10, 10)));
        container.add(createFormLine("Phone:"));
        container.add(Box.createRigidArea(new Dimension(10, 10)));
        container.add(createFormLine("Gender:"));
        container.add(Box.createVerticalGlue());

        var loginBtn = new JButton("Login");
        loginBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(loginBtn);
    }

    private void showWindow() {
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        var screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenSize.width - WIDTH) / 2, (screenSize.height - HEIGHT) / 2);
        setVisible(true);
    }
}
